package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.service.ApiClazzService;
import com.lanou3g.ptp.entity.Clazz;
import com.lanou3g.ptp.organize.service.ClazzService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "班级接口")
@RestController
public class ClazzController {
    @Autowired
    private ApiClazzService apiClazzService;
    @Reference
    private ClazzService clazzService;
    @ApiOperation("查询所有班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数"),
            @ApiImplicitParam(name = "size",value = "显示条数")
    })
    @GetMapping("/clazz")
    public PtpResult findAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10")Integer size) {
        PageHelper.startPage(page, size);
        PageInfo<Object> pageInfo = apiClazzService.findAll(page, size);
        return PtpResultUtils.ok(pageInfo);
    }

    @ApiOperation("添加班级")
    @PostMapping("/clazz")
    public PtpResult addCollege(Clazz clazz){
        clazzService.addClazz(clazz);
        return PtpResultUtils.ok(23333);
    }

    @ApiOperation("通过id批量删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "班级id")
    })
    @DeleteMapping("clazzes/{ids}")
    public PtpResult deleteByIds(@PathVariable Long[] ids){
        clazzService.deleteByIds(ids);
        return PtpResultUtils.ok(23333);
    }

    @ApiOperation("通过id修改班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "班级id")
    })
    @PutMapping("/clazz")
    public PtpResult updateById(@Param("id") Long id, @Param("clazz") Clazz clazz){
        clazzService.updateById(id,clazz);
        return PtpResultUtils.ok(2333);
    }

    @ApiOperation("通过id删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "班级id")
    })
    @DeleteMapping("clazz/{id}")
    public PtpResult deleteById(@PathVariable Long id){
        clazzService.deleteById(id);
        return PtpResultUtils.ok(2333);
    }

    @ApiOperation("通过name查找班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "班级名称")
    })
    @GetMapping("/clazz/{name}")
    public PtpResult findByName(@PathVariable String name){
        PtpResult clazz = apiClazzService.findByName(name);
        return PtpResultUtils.ok(clazz);
    }
}
